/**
 * VimL字符串操作示例
 * 
 * 本示例展示了VimL虚拟机中的字符串操作功能，包括：
 * 1. 字符串连接操作(..)
 * 2. 字符串与数字的混合使用
 * 3. 循环中的字符串构建
 * 4. 复杂数据结构中的字符串应用
 * 
 * 编译和运行：
 * mkdir -p build && cd build
 * cmake .. && ninja
 * ./bin/string_operations_example
 * 
 * 作者：VimL虚拟机项目组
 * 日期：2025-09-27
 */

#include "viml/compiler.h"
#include "viml/vm.h"
#include <iostream>

using namespace viml;

void print_header(const std::string& title) {
    std::cout << "\n" << std::string(50, '=') << std::endl;
    std::cout << " " << title << std::endl;
    std::cout << std::string(50, '=') << std::endl;
}

int main() {
    try {
        Compiler compiler;
        VM vm;
        
        print_header("VimL字符串操作示例");
        
        // 基本字符串操作
        std::string test1 = R"(
let greeting = "Hello"
let target = "VimL"
let message = greeting .. " " .. target .. "!"
echo message

let version = 1
let build = 0
let version_str = "Version " .. version .. "." .. build
echo version_str
)";
        
        std::cout << "执行基本字符串操作..." << std::endl;
        auto bytecode1 = compiler.compile(test1, "basic_strings.vim");
        vm.execute(bytecode1);
        
        // 重置编译器和虚拟机
        compiler.reset();
        vm.reset();
        
        // 循环中的字符串操作
        std::string test2 = R"(
function build_sequence(count)
    let sequence = "Numbers: "
    let i = 1
    while i <= count
        let sequence = sequence .. i
        if i < count
            let sequence = sequence .. ", "
        endif
        let i = i + 1
    endwhile
    return sequence
endfunction

let result = build_sequence(5)
echo result
)";
        
        std::cout << "\n执行循环中的字符串操作..." << std::endl;
        auto bytecode2 = compiler.compile(test2, "loop_strings.vim");
        vm.execute(bytecode2);
        
        // 重置编译器和虚拟机
        compiler.reset();
        vm.reset();
        
        // 复杂数据结构中的字符串
        std::string test3 = R"(
function create_objects()
    let objects = []
    let i = 1
    while i <= 3
        let obj = {"id": i, "value": "object_" .. i}
        echo "Created object with value:", obj["value"]
        let objects = objects + [obj]
        let i = i + 1
    endwhile
    return objects
endfunction

let result = create_objects()
echo "Created", len(result), "objects"
)";
        
        std::cout << "\n执行复杂数据结构中的字符串操作..." << std::endl;
        auto bytecode3 = compiler.compile(test3, "complex_strings.vim");
        vm.execute(bytecode3);
        
        print_header("字符串操作测试完成");
        std::cout << "✓ 基本字符串连接：正常" << std::endl;
        std::cout << "✓ 字符串与数字混合：正常" << std::endl;
        std::cout << "✓ 循环中的字符串构建：正常" << std::endl;
        std::cout << "✓ 复杂数据结构中的字符串：正常" << std::endl;
        std::cout << "\n🎉 所有字符串操作测试通过！" << std::endl;
        
        return 0;
        
    } catch (const std::exception& e) {
        std::cout << "\n❌ 字符串操作测试失败: " << e.what() << std::endl;
        return 1;
    }
}